package com.woniu.shop.controller;

import com.woniu.PageResult;
import com.woniu.Result;
import com.woniu.shop.pojo.ServiceItem;
import com.woniu.shop.pojo.vo.ServiceItemVo;
import com.woniu.shop.rabibit.ItemProduct;
import com.woniu.shop.service.IServiceItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaohuang
 * @since 2022-11-16
 */
@RestController
@RequestMapping("/service")
@Api(tags = "发布项目管理")
public class ServiceItemController {
    @Autowired
    private IServiceItemService service;

    @Autowired
    private ItemProduct itemProduct;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("添加")
    @PostMapping
    public Result<String> saveServiceItem(@RequestBody ServiceItem serviceItem){

        boolean save = service.save(serviceItem);
//        通过mq给后台发送服务项目id
        itemProduct.sendLog(serviceItem.getId());

        stringRedisTemplate.opsForSet().add("redis_db_upload_image",serviceItem.getProjectImg());
        return save ? Result.ok():Result.fail();
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<String> updateServiceItem(@RequestBody ServiceItem serviceItem){
        //        通过mq给后台发送服务项目id
        itemProduct.sendLog(serviceItem.getId());
        Result result = service.updateServiceItem(serviceItem);
        return result;
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Result<String> deleteServiceItem(@PathVariable Integer id){
        boolean b = service.removeById(id);
        return b ? Result.ok():Result.fail();
    }

    @ApiOperation("分页查询")
    @PostMapping("/page/{currentPage}/{pageSize}")
    public Result<PageResult> pageSearch(@RequestBody(required = false)ServiceItemVo serviceItemVo,
                                   @PathVariable Integer currentPage,
                                   @PathVariable Integer pageSize){
        Result<PageResult> result = service.pageSearch(serviceItemVo,currentPage,pageSize);

        return result;
    }

    @ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public Result<ServiceItem> getById(@PathVariable Integer id){
        ServiceItem serviceItem = service.getById(id);
        return Result.ok(serviceItem);
    }

    @ApiOperation("项目审核发布")
    @PutMapping("/audit")
    public Result<String> audit(@RequestBody ServiceItem serviceItem){
        Result<String> result = service.audit(serviceItem);
        return result;
    }
}
